package com.fly.caihong.view.loading;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatDialog;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.fly.caihong.R;


/**
 * 加载对话框
 */
public class FLoadingDialog extends AppCompatDialog {

    private TextView mTvMsg;
    private LoadingIndicatorView mAVLoadingView;

    public FLoadingDialog(Context context) {
        super(context, R.style.DialogStyle);
        initialize(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void initialize(Context context) {
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, null);
        mTvMsg = view.findViewById(R.id.tv_loading_dialog_text);
        mAVLoadingView = view.findViewById(R.id.av_loading_view);
        setContentView(view);
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 显示加载提示
     *
     * @param msg
     */
    public void show(String msg) {
        mTvMsg.setText(msg);
        mAVLoadingView.smoothToShow();
        super.show();
    }

    /**
     * 显示加载中
     */
    public void show() {
        mTvMsg.setText("正在加载中...");
        mAVLoadingView.smoothToShow();
        super.show();
    }

    /**
     * 设置提示内容
     *
     * @param msg
     */
    public void setLoadingMsg(String msg) {
        mTvMsg.setText(msg);
    }

    /**
     * 取消
     */
    public void dismiss() {
        super.dismiss();
        mAVLoadingView.smoothToHide();
    }

}
